package com.loby.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.loby.data.base.pojo.*;
import com.loby.data.code.pojo.TCodeAdministrativeDivision;
import com.loby.data.history.pojo.*;
import com.loby.data.mapper.*;
import com.loby.data.redis.pojo.RedisRealData;
import com.loby.data.rest.ErrorCode;
import com.loby.data.state.pojo.StationState;
import com.loby.data.util.*;
import com.loby.data.view.pojo.VStationTree;
import com.loby.monitor.client.propertie.ClientProperty;
import com.loby.util.RedisOperator;
import com.loby.zk.util.PropertyUtil;
import com.loby.zk.zkeeper.BaseZookeeper;
import com.loby.data.rest.RestResult;
import com.loby.service.api.BaseMonitorDataServiceApi;
import lombok.extern.slf4j.Slf4j;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.*;
import java.util.stream.Collectors;


/**
 * 操作基础信息，插入排放口等
 */
@Service
@Component
@Slf4j
public class BaseMonitorDataServiceApiImpl implements BaseMonitorDataServiceApi {
    private static final int MIN_CORP_LEN = 6;


    @Autowired
    private OrganizationMapper organizationMapper;
    @Autowired
    private SourceShopsMapper sourceShopsMapper;
    @Autowired
    private MonitorStationMapper monitorStationMapper;

    @Autowired
    private AdministrativeDivisionMapper administrativeDivisionMapper;

    @Autowired
    private MonitorStationPollutantMapper monitorStationPollutantMapper;

    @Autowired
    private DataTableMapper dataTableMapper;

    @Autowired
    private  PollutantMapper pollutantMapper;

    @Autowired
    private RedisOperator redis;


    @Autowired
    private BaseZookeeper zookeeper;

    @Override
    public RestResult newOrganization(TBaseOrganization organization) {
        long id = 0;
        boolean b = false;
        try {
            //beginTranscation()
            id = organizationMapper.insertOrganization(organization);
            b = zookeeper.newOrganization(organization);
            System.out.println(organization.getPkId());
            //commit
        } catch (Exception e) {
            //rollback();
            log.info("数据库异常", e);
            return RestResult.errorOf(e.getLocalizedMessage());
        }
        if (id > 0 && b) {
            return RestResult.success("新增成功");
        }

        return RestResult.errorOf("新增失败");
    }


    @Override
    public RestResult removeOrganization(long id) {
        TBaseOrganization organization = organizationMapper.getOrganizationById(id);
        if (organization != null) {
            try {
                int i = organizationMapper.removeOrganization(id);
                if (i > 0 && zookeeper.removeOrganization(organization)) {
                    return RestResult.successMessageOf("删除成功");
                } else {
                    log.warn("数据库删除成功，zk删除失败");
                    return RestResult.errorOf("数据库删除成功，zk删除失败");
                }
            } catch (Exception e) {
                log.warn(e.getLocalizedMessage());
                return RestResult.errorOf(e.getLocalizedMessage());
            }
        } else {
            return RestResult.errorOf(ErrorCode.PARAMS_ERROR, "删除的对象不能为空");
        }
    }

    @Override
    public RestResult updateOrganization(TBaseOrganization organization) {
        int l = 0;
        try {
            l = organizationMapper.updateOrganization(organization);
        } catch (Exception e) {
            log.info("数据库错误");
            return RestResult.errorOf(ErrorCode.PARAMS_ERROR, e.getLocalizedMessage());
        }

        if (l > 0 && zookeeper.updateOrganizationName(organization)) {
            return RestResult.success("修改成功");
        }
        return RestResult.errorOf("修改失败");
    }


    @Override
    public RestResult newShop(TMonSourceShops shop) {
        int l = 0;
        TBaseOrganization organization = null;
        try {
            l = sourceShopsMapper.insertShop(shop);
            organization = organizationMapper.getOrganizationById(shop.getFkPsId());
        } catch (Exception e) {
            log.info("数据库错误！");
            return RestResult.errorOf(ErrorCode.PARAMS_ERROR, e.getLocalizedMessage());
        }

        if (l > 0 && zookeeper.newShop(organization, shop)) {
            return RestResult.builder().message("添加车间成功").value(l).code(ErrorCode.SUCCESS.getCode()).build();
        }
        return RestResult.builder().message("添加车间失败").value(l).code(ErrorCode.UNKNOW_ERROR.getCode()).build();
    }

    @Override
    public RestResult removeShop(long id) {
        TMonSourceShops shop = sourceShopsMapper.getShopByPkId(id);
        if (shop != null) {
            int l = 0;
            TBaseOrganization organization = null;
            try {
                monitorStationMapper.removeMonitorStationByShopId(shop.getPkId());
                l = sourceShopsMapper.removeShop(id);
                organization = organizationMapper.getOrganizationById(shop.getFkPsId());
            } catch (Exception e) {
                log.info("数据库错误", e);
                return RestResult.errorOf(ErrorCode.PARAMS_ERROR, e.getLocalizedMessage());
            }
            if (l > 0 && zookeeper.removeShop(organization, shop)) {
                return RestResult.success("删除车间成功");
            } else {
                return RestResult.errorOf("车间删除失败");
            }
        }

        return RestResult.errorOf("车间不能为空");
    }

    @Override
    public RestResult updateShop(TMonSourceShops shop) {
        long l = 0;
        try {
            l = sourceShopsMapper.updateShop(shop);
            TBaseOrganization organization = organizationMapper.getOrganizationById(shop.getFkPsId());
            if (l > 0 && zookeeper.updateShop(shop, organization)) {
                return RestResult.success("修改车间名称成功");
            }
        } catch (Exception e) {
            log.info("数据库错误");
            return RestResult.errorOf(ErrorCode.PARAMS_ERROR, e.getLocalizedMessage());
        }
        return RestResult.errorOf("修改车间失败");
    }

    @Override
    public RestResult newMonitorStation(TMonMonitorStation monitorStation) {
        TMonMonitorStation station = null;
        TMonSourceShops shop = null;
        TBaseOrganization organization = null;
        List<MHisDataTables> tables = null;
        List<TMonForwardInfos> infos = null;
        int l = 0;
        try {
            l = monitorStationMapper.newMonitorStation(monitorStation);
            station = monitorStationMapper.getMonitorStationByCode(monitorStation.getStationUniqueCode());
            dataTableMapper.newCodRealTable(station.getPkId(), station.getStationUniqueCode());
            dataTableMapper.newCodMinTable(station.getPkId(), station.getStationUniqueCode());
            dataTableMapper.newCodHourTable(station.getPkId(), station.getStationUniqueCode());
            dataTableMapper.newCodDayTable(station.getPkId(), station.getStationUniqueCode());
            shop = sourceShopsMapper.getShopByPkId(station.getFkShopId());
            organization = organizationMapper.getOrganizationById(shop.getFkPsId());
            tables = dataTableMapper.getTablesByStationId(station.getPkId());
            infos = monitorStationMapper.getForwardInfosByStationId(station.getPkId());

        } catch (Exception e) {
            log.info("数据库异常!", e);
            return RestResult.errorOf(e.getLocalizedMessage());
        }
        if (!CollectionUtils.isEmpty(tables) && !CollectionUtils.isEmpty(infos)) {
            ClientProperty property = PropertyUtil.getProperty(station, tables, infos);
            boolean b = zookeeper.newMonitorStation(monitorStation, shop, organization, property.toJosn());
            if (l > 0 && b) {
                return RestResult.success("新增排放口成功!");
            }
        }
        return RestResult.errorOf("新增排放口失败");

    }

    @Override
    public RestResult removeMonitorStation(long id) {
        TMonMonitorStation station = monitorStationMapper.getMonitorStationById(id);
        if (station != null) {
            TMonSourceShops shop = null;
            TBaseOrganization organization = null;
            int l = 0;
            try {
                shop = sourceShopsMapper.getShopByPkId(station.getFkShopId());
                organization = organizationMapper.getOrganizationById(shop.getFkPsId());
                l = monitorStationMapper.removeMonitorStationById(id);

            } catch (Exception e) {
                log.info("数据库异常!", e);
                return RestResult.errorOf(e.getLocalizedMessage());
            }
            if (l > 0 && zookeeper.removeMonitorStation(station, shop, organization)) {
                return RestResult.errorOf("删除排放口成功");
            } else {
                return RestResult.errorOf("删除排放口失败");
            }
        }
        return RestResult.errorOf("排放口不存在！");
    }

    @Override
    public RestResult updateByMonitorStationId(long id) {
        TMonMonitorStation station = monitorStationMapper.getMonitorStationById(id);
        if (station != null) {
            TBaseOrganization organization = null;
            TMonSourceShops shop = null;
            List<MHisDataTables> tables = null;
            List<RtMonStationPollutant> pollutants = null;
            List<TMonForwardInfos> infos = null;
            try {
                shop = sourceShopsMapper.getShopByPkId(station.getFkShopId());
                organization = organizationMapper.getOrganizationById(shop.getFkPsId());
                tables = dataTableMapper.getTablesByStationId(station.getPkId());
                pollutants = pollutantMapper.getPollutantByMonStationId(station.getPkId());
                infos = monitorStationMapper.getForwardInfosByStationId(station.getPkId());
            } catch (Exception e) {
                log.info("数据库异常!", e);
                return RestResult.errorOf(e.getLocalizedMessage());
            }
            ClientProperty property = PropertyUtil.getProperty(station, tables, infos);
            PropertyUtil.addCondition(property, pollutants);
            if (zookeeper.updateByMonitorStationId(organization, shop, station, property.toJosn())) {
                return RestResult.success("更新成功");
            } else {
                return RestResult.errorOf("更新失败");
            }
        }

        return RestResult.errorOf("排放口不存在！");
    }


    /**
     * 返回三层行政区划树
     *
     * @param keys 排放口ID列表
     * @return 行政區劃樹
     */
    @Override
    public RestResult getStationTree(int typeId, String keys) {
        List<BaseTree> baseTrees = null;

        try {
            if (keys != null && !keys.isEmpty()) {
                List<VStationTree> monitorStationTree = monitorStationMapper.getMonitorStationTree(keys);
                ArrayList<TCodeAdministrativeDivision> dists = new ArrayList<>();
                ArrayList<TBaseOrganization> orgs = new ArrayList();
                for (int i = 0; i < monitorStationTree.size(); i++) {
                    if (monitorStationTree.get(i).getFkStationCategoryCode() != typeId) {
                        monitorStationTree.remove(i);
                        i--;
                    }
                }
                for (VStationTree vStationTree : monitorStationTree) {

                    List<TCodeAdministrativeDivision> adminis = administrativeDivisionMapper.getAdministrativeDivisionsAllParents(vStationTree.getAdCode());
                    orgs.add(organizationMapper.getOrganizationById(vStationTree.getPsPkId()));
                    dists.addAll(adminis);
                }

                List<BaseTree> roots = dists.stream().map(c -> new BaseTree()
                        .setKey(c.getPkDivisionCode())
                        .setPkId(c.getPkDivisionCode())
                        .setParentKey(c.getFkParentCode())
                        .setTitle(c.getDivisionName()))
                        .collect(Collectors.toList());
                if (orgs != null) {
                    orgs.forEach(s -> {
                        roots.add(new BaseTree()
                                .setTitle(s.getOrgName())
                                .setKey(s.getPkOrgCode())
                                .setParentKey(s.getFkAdministrativeDivisionCode())
                                .setUniqueCode(s.getPkOrgCode())
                                .setPkId(String.valueOf(s.getPkId()))
                                .setFlag(s.getOrgName())
                        );
                    });
                }
                monitorStationTree.forEach(a -> {
                    StationState state = getStationState(a.getStationUniqueCode());
                    roots.add(new BaseTree()
                            .setTitle(a.getStationName())
                            .setPkId(String.valueOf(a.getStPkId()))
                            .setKey(String.valueOf(a.getStPkId()))
                            .setParentKey(String.valueOf(a.getPkOrgCode()))
                            .setStatus(state != null ? state.getState() : 0)
                            .setUniqueCode(a.getStationUniqueCode())
                    );
                });
                baseTrees = BaseTree.buildTree(roots);
                BaseTree children = getChildren(baseTrees.get(0));
                TreeParam keyParam = new TreeParam();
                keyParam.setFilstSKey(children.getKey());
                keyParam.setFilstPKey(baseTrees.get(0).getKey());
                keyParam.setTrees(baseTrees);
                return RestResult.success(keyParam);
            }

        } catch (Exception e) {
            log.error("内部错误！", e.getLocalizedMessage());
            return RestResult.successMessageOf("无数据！");

        }
        return RestResult.successMessageOf("无数据!");
    }


    @Override
    public RestResult newRtStationPollutant(RtMonStationPollutant pollutant) {
        TMonMonitorStation station = null;
        TMonSourceShops shop = null;
        TBaseOrganization organization = null;
        List<RtMonStationPollutant> pollutants = null;
        try {
            int i = monitorStationPollutantMapper.insertRtStationPollutants(pollutant);
            if (i > 0) {
                station = monitorStationMapper.getMonitorStationById(pollutant.getFkMonitorId());
                shop = sourceShopsMapper.getShopByPkId(station.getFkShopId());
                organization = organizationMapper.getOrganizationById(shop.getFkPsId());
                pollutants = monitorStationPollutantMapper.getPollutantByMonStationId(station.getPkId());

                if (zookeeper.addExcess(organization, shop, station, pollutants)) {
                    return RestResult.success("新增因子超标信息成功");
                }
            }
        } catch (Exception e) {
            log.warn("数据库异常!" + e.getLocalizedMessage());
            return RestResult.errorOf(e.getLocalizedMessage());
        }
        return RestResult.errorOf("新增因子超标信息失败");
    }


    @Override
    public RestResult getMultipulStationByCode(String code, int typeId, String keys) {
        try {
            if (code.length() == MIN_CORP_LEN) {
                List<VStationTree> stations = monitorStationMapper.getChildStationsByAdCode(code, keys);
                if (stations != null && stations.size() != 0) {
                    List<VStationTree> nStations = stations.stream().filter(s -> s.getFkStationCategoryCode() == typeId).collect(Collectors.toList());
                    for (VStationTree station : nStations) {
                        RedisRealData polltant = getPolltants(station.getFkPollutionCategoryCode(), station.getStationUniqueCode());
                        station.setPollutions(polltant);
                        station.setPollutionsNumber(polltant.getData().size());
                    }
                    return RestResult.success(stations);
                }
                return RestResult.success("无排放口");
            } else if (code.length() > MIN_CORP_LEN) {
                List<VStationTree> stations = monitorStationMapper.getStationsByOrgCode(code, keys);
                List<VStationTree> nStations = stations.stream().filter(s -> s.getFkStationCategoryCode() == typeId).collect(Collectors.toList());
                if (!CollectionUtils.isEmpty(nStations)) {
                    for (VStationTree station : nStations) {
                        RedisRealData polltant = getPolltants(station.getFkPollutionCategoryCode(), station.getStationUniqueCode());
                        station.setPollutions(polltant);
                        station.setPollutionsNumber(polltant.getData().size());
                    }
                    return RestResult.success(stations);
                }
                return RestResult.success("无排放口");

            } else {
                VStationTree station = monitorStationMapper.getStationByStId(Long.parseLong(code));
                ArrayList list = new ArrayList();
                if (station != null) {
                    RedisRealData polltant = getPolltants(station.getFkPollutionCategoryCode(), station.getStationUniqueCode());
                    station.setPollutions(polltant);
                    station.setPollutionsNumber(polltant.getData().size());
                    list.add(station);
                    return RestResult.success(list);
                }
                return RestResult.successMessageOf("无排放口");
            }
        } catch (Exception e) {
            log.info("内部错误", e.getLocalizedMessage());
            return RestResult.successMessageOf("无数据");
        }

    }

    @Override
    public RestResult getAllStations(int typeId, String keys) {
        log.info("===" + keys);
        try {
            List<VStationTree> list = monitorStationMapper.getMonitorStationTree(keys);
            if (list != null) {
                List<VStationTree> stations = list.stream().filter(s -> s.getFkStationCategoryCode() == typeId).collect(Collectors.toList());
                for (VStationTree station : stations) {
                    RedisRealData polltant = getPolltants(station.getFkPollutionCategoryCode(), station.getStationUniqueCode());
                    StationState state = getStationState(station.getStationUniqueCode());
                    station.setPollutions(polltant);
                    station.setStatus(state != null ? state.getState():0);
                    station.setPollutionsNumber(polltant!=null&&polltant.getData()!=null?polltant.getData().size():0);
                }
                return RestResult.success(stations);
            }
        } catch (Exception e) {
            log.error(e.getLocalizedMessage());
            return RestResult.success("无数据!");
        }
        return RestResult.success("排放口为空!");
    }


    /**
     * @param mnCodes
     * @param keys
     * @return
     */
    @Override
    public RestResult getMultipulStationsByMNCodes(String[] mnCodes, String keys) {
        ArrayList list = new ArrayList();
        List<TCodeAdministrativeDivision> parents = null;
        try {
            for (String code : mnCodes) {
                VStationTree station = monitorStationMapper.getStationByStId(Long.parseLong(code));
                if (station != null) {
                    parents = administrativeDivisionMapper.getAdministrativeDivisionsAllParents(station.getAdCode());
                    for (TCodeAdministrativeDivision parent : parents) {
                        if (parent.getLevel() == 1) {
                            station.setKey(parent.getPkDivisionCode());
                        }
                    }
                    RedisRealData polltant = getPolltants(station.getFkPollutionCategoryCode(), station.getStationUniqueCode());
                    station.setPollutions(polltant);
                    station.setPollutionsNumber(polltant!=null&&polltant.getData()!=null?polltant.getData().size():0);
                    list.add(station);
                }
            }
            if (CollectionUtils.isEmpty(list)) {
                return RestResult.successMessageOf("无数据!");
            }
            return RestResult.success(list);

        } catch (Exception e) {
            log.info("内部错误", e.getLocalizedMessage());
            return RestResult.successMessageOf("无数据");
        }

    }

    @Override
    public RestResult getStationById(long id) {
        ArrayList list = null;
        VStationTree station = null;
        RedisRealData polltants = null;
        List<HisRealData> charDatas = null;
        try {
            station = monitorStationMapper.getStationByStId(id);
            if (station != null) {
                polltants = getPolltants(station.getFkPollutionCategoryCode(), station.getStationUniqueCode());
                charDatas = monitorStationMapper.getCharDatas(station.getStationUniqueCode(), polltants.getTime());
                if (polltants != null) {
                    station.setPollutions(polltants);
                    station.setPollutionsNumber(polltants.getData().size());
                    list = new ArrayList(1);
                    list.add(station);
                    if (charDatas != null) {
                        station.setChartData(charDatas);
                    }
                }
                return RestResult.success(list);
            }
        } catch (Exception e) {
            log.warn("getStationById内部错误" + e.getLocalizedMessage());
            return RestResult.success("无数据!");
        }
        log.error("获取排放口或对应污染因子失败！");
        return RestResult.success("无数据!");
    }

    @Override
    public RestResult getMinDatas(TimeDataParam param) {
        List<HisMinData> datas = null;
        //    ArrayList list = new ArrayList(1);
        try {
            datas = monitorStationMapper.getMinDatas(param.getMnCode(), param.getStartTime(), param.getEndTime());
     /*       for (HisMinData data : datas) {
                CommonsData2 data2 = new CommonsData2();
                ArrayList list2 = new ArrayList();
                list2.add(NewData.builder().id("w01001").Name("PH").value(data.getW01001()).falg(data.getFlagW01001()).Unit("").build());
                list2.add(NewData.builder().id("w00000").Name("污水").value(data.getW00000()).falg(data.getFlagW00000()).Unit("").build());
                data2.setDatas(list2);
                data2.setDataTime(data.getDataTime());
                data2.setPkDataId(data.getPkDataId());
                list.add(data2);
            }*/
            if (!CollectionUtils.isEmpty(datas)) {
                VStationTree station = monitorStationMapper.getStationByMnCode(param.getMnCode());
                CommonsData minData = CommonsData.builder().orgName(station.getOrgName()).stationName(station.getStationName()).datas(datas).build();
                return RestResult.success(minData);
            }
        } catch (Exception e) {
            log.error("内部错误", e.getLocalizedMessage());
            return RestResult.successMessageOf("无数据!");
        }
        return RestResult.successMessageOf("无数据!");
    }

    @Override
    public RestResult getRealDatas(TimeDataParam param) {
        List<HisRealData> datas = null;

        try {
            datas = monitorStationMapper.getRealDatas(param.getMnCode(), param.getStartTime(), param.getEndTime());

            if (!CollectionUtils.isEmpty(datas)) {
                VStationTree station = monitorStationMapper.getStationByMnCode(param.getMnCode());
                CommonsData minData = CommonsData.builder().orgName(station.getOrgName()).stationName(station.getStationName()).datas(datas).build();

                return RestResult.success(minData);
            }
        } catch (Exception e) {
            log.error("内部错误", e.getLocalizedMessage());
            return RestResult.successMessageOf("无数据");
        }
        return RestResult.successMessageOf("无数据");
    }

    @Override
    public RestResult getHourDatas(TimeDataParam param) {
        List<HisHourData> datas = null;
        try {
            datas = monitorStationMapper.getHourDatas(param.getMnCode(), param.getStartTime(), param.getEndTime());
            if (!CollectionUtils.isEmpty(datas)) {
                VStationTree station = monitorStationMapper.getStationByMnCode(param.getMnCode());
                CommonsData hourData = CommonsData.builder().orgName(station.getOrgName()).stationName(station.getStationName()).datas(datas).build();

                return RestResult.success(hourData);
            }
        } catch (Exception e) {
            log.error("内部错误", e.getLocalizedMessage());
            return RestResult.successMessageOf("无数据");
        }
        return RestResult.successMessageOf("无数据");
    }

    @Override
    public RestResult getDayDatas(TimeDataParam param) {
        List<HisDayData> datas = null;
        try {
            datas = monitorStationMapper.getDayDatas(param.getMnCode(), param.getStartTime(), param.getEndTime());
            if (!CollectionUtils.isEmpty(datas)) {
                VStationTree station = monitorStationMapper.getStationByMnCode(param.getMnCode());
                CommonsData dayData = CommonsData.builder().orgName(station.getOrgName()).stationName(station.getStationName()).datas(datas).build();
                return RestResult.success(dayData);
            }
        } catch (Exception e) {
            log.error("内部错误", e.getLocalizedMessage());
            return RestResult.successMessageOf("无数据");
        }
        return RestResult.successMessageOf("无数据");
    }

    @Override
    public RestResult getAllTimeDatas(String mnCode) {
        try {
            VStationTree station = monitorStationMapper.getStationByMnCode(mnCode);
            List<HisRealData> allRealDatas = monitorStationMapper.getAllRealDatas(mnCode);
            List<HisMinData> allMinDatas = monitorStationMapper.getAllMinDatas(mnCode);
            List<HisHourData> allHourDatas = monitorStationMapper.getAllHourDatas(mnCode);
            List<HisDayData> allDayDatas = monitorStationMapper.getAllDayDatas(mnCode);
            ArrayList list = new ArrayList();
            list.addAll(allRealDatas);
            list.addAll(allMinDatas);
            list.addAll(allHourDatas);
            list.addAll(allDayDatas);
            CommonsData allData = CommonsData.builder().orgName(station.getOrgName()).stationName(station.getStationName()).datas(list).build();
            return RestResult.success(allData);
        } catch (Exception e) {
            log.error("内部错误", e.getLocalizedMessage());
            return RestResult.errorOf("无数据！");
        }

    }


    public RedisRealData getPolltants(String ptCode, String mnCode) {
        List realDatas = null;
        try {
            realDatas = redis.getRealDatas(ptCode, mnCode);
            RedisRealData realData = JSONObject.parseObject(JSONObject.toJSONString(realDatas.get(0)), RedisRealData.class);
            return realData;
        } catch (Exception e) {
            log.warn("GetPollutants Failed:ptCode[" + ptCode + "]:mnCode[" + mnCode + "]");
            return null;
        }
    }

    public StationState getStationState(String mnCode) {
        StationState state = null;
        try {
            List status = redis.getClientStatus(mnCode);
            List list= (List) status.stream().filter(s -> JSONObject.toJSONString(s).contains("State") || JSONObject.toJSONString(s).contains("OnLine")).collect(Collectors.toList());

        } catch (Exception e) {
        log.error(e.getLocalizedMessage());
        return null;
        }
        System.out.println(state);
        return state;
    }


    public BaseTree getChildren(BaseTree baseTree) {
        if (!ObjectUtils.isEmpty(baseTree.getChildren())) {
            for (BaseTree child : baseTree.getChildren()) {
                return getChildren(child);
            }
        }
        return baseTree;
    }






}
